package com.example.new6.client;

import android.content.Context;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ApiClient {
    OkHttpClient client = new OkHttpClient();
    private OnDataListener onDataListener;

    public interface OnDataListener {
        void onDataReceived(String hanzi, String pinyin, String bihua, String bushou, String words, String basicExplain, String detailExplain);
        void onUrlReceived(String url);
    }

    public void fetchData(final Context context, String url, final OnDataListener listener) {
        Request request = new Request.Builder()
                .url(url)
                .build();

        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String jsonData = response.body().string();
                    JsonObject jsonObject = JsonParser.parseString(jsonData).getAsJsonObject();
                    if (listener != null) {
                        // 检查 "result" 字段是否存在并且不是 JsonNull
                        if (jsonObject.has("result") && !jsonObject.get("result").isJsonNull()) {
                            JsonObject resultObject = jsonObject.getAsJsonObject("result");
                            String hanzi = resultObject.get("hanzi").getAsString();
                            String pinyin = resultObject.get("pinyin").getAsString();
                            String bihua = resultObject.get("bihua").getAsString();
                            String bushou = resultObject.get("bushou").getAsString();
                            String words = resultObject.get("words").getAsString();

                            // 解析基本解释数组
                            JsonArray basicExplainArray = resultObject.getAsJsonArray("basic_explain");
                            StringBuilder basicExplainBuilder = new StringBuilder();
                            for (int i = 0; i < basicExplainArray.size(); i++) {
                                basicExplainBuilder.append(basicExplainArray.get(i).getAsString());
                                if (i < basicExplainArray.size() - 1) {
                                    basicExplainBuilder.append("\n");
                                }
                            }
                            String basicExplain = basicExplainBuilder.toString();

                            // 解析详细解释数组
                            JsonArray detailExplainArray = resultObject.getAsJsonArray("detail_explain");
                            StringBuilder detailExplainBuilder = new StringBuilder();
                            for (int i = 0; i < detailExplainArray.size(); i++) {
                                detailExplainBuilder.append(detailExplainArray.get(i).getAsString());
                                if (i < detailExplainArray.size() - 1) {
                                    detailExplainBuilder.append("\n");
                                }
                            }
                            String detailExplain = detailExplainBuilder.toString();

                            listener.onDataReceived(hanzi, pinyin, bihua, bushou, words, basicExplain, detailExplain);
                        } else {
                            // 处理 "result" 字段不存在或为 JsonNull 的情况
                        }

                        if (jsonObject.has("url") && !jsonObject.get("url").isJsonNull()) {
                            String url = jsonObject.get("url").getAsString();
                            listener.onUrlReceived(url);
                        }
                    }
                }
            }
        });
    }

    public void setOnDataListener(OnDataListener listener) {
        this.onDataListener = listener;
    }
}